/*Copyright (c) Microsoft Open Technologies, Inc.  All rights reserved.
Microsoft Open Technologies would like to thank its contributors, a list
of whom are at https://interview.codeplex.com/team/views.

Licensed under the Apache License, Version 2.0 (the "License"); you
may not use this file except in compliance with the License. You may
obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
implied. See the License for the specific language governing permissions
and limitations under the License.*/

#include <iostream>
#include <string>
#include <vector>
#include <sstream>
#include "list.h"
using namespace std;

// Problem: the problem is to transform a singly link list, move first k items to the end of the list
// e.g. 11->23->44->68->36->71->88, k=3
// result: 68->36->71->88->11->23->44
// As you seen, the first 3 items are moved to the end of the list.
void big_transform(list &l, uint32_t k) {
    uint32_t index = 1;
    node *ret = l.root, *temp = l.root;

    // To identify the tail node for the new list, temp is the tail in the new list.
    while (temp != nullptr && index < k) {
        if (temp->next != nullptr) {
            temp = temp->next;
            ++index;
        }
        else {
            break;
        }
    }

    if (index < k) {
        cout << "the length of the list is less than: " << k << endl;
        return;
    }

    // Since index == k, and temp->next == nullptr, that means the list contains only k items.
    if (temp->next == nullptr) {
        // length of list equals to k.
        return;
    }

    l.root = temp->next;
    temp->next = nullptr;
    temp = l.root;

    while (temp->next != nullptr) {
        temp = temp->next;
    }

    temp->next = ret;
}

int main() {
    list l;
    string str;
    getline(cin, str);
    node **pp = &l.root;

    stringstream ss;
    ss << str;

    int data;

    while (ss >> data) {
        *pp = new node(data);
        pp = &((*pp)->next);
    }

    l.print();

    int transform_count;
    cin >> transform_count;

    big_transform(l, transform_count);

    l.print();

    return 0;
}